package com.ty.service.impl;

import com.ty.service.LookupService;
import com.ty.utils.IpUtil;
import com.ty.utils.UserAgentUtil;
import com.ty.utils.WebUtil;
import com.ty.utils.ip.IPLocation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * IP归属地查询业务逻辑实现类
 *
 * @Author Tommy
 * @Date 2022/10/23
 */
@Service
@Slf4j
public class LookupServiceImpl implements LookupService {

    @Override
    public IPLocation lookup(String ip) throws Exception {
        IPLocation ipLocation = null;
        if (IpUtil.checkIP(ip)) {
            if (IpUtil.internalIp(ip)) {
                ipLocation = new IPLocation("局域网", "对方和您在同一内部网", ip);
            } else {
                ipLocation = IpUtil.getLocation(ip);
            }
        }
        if (null != ipLocation) {
            ipLocation.setOs(UserAgentUtil.getOSName(WebUtil.getUserAgent()));
        }
        log.debug("IP归属地：" + ipLocation);
        return ipLocation;
    }
}
